/*
 *
 *  *    Copyright 2020-2021 luter.me
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

package com.luter.heimdall.boot.web.resolver;


import com.luter.heimdall.core.annotation.CurrentToken;
import com.luter.heimdall.core.exception.HeimdallException;
import com.luter.heimdall.core.manager.AuthenticationManager;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * 实现注解:CurrentToken
 *
 * @author luter
 */
@RequiredArgsConstructor
public class CurrentTokenRequestArgumentResolver implements HandlerMethodArgumentResolver {
    /**
     * The constant log.
     */
    private static final transient Logger log = getLogger(CurrentTokenRequestArgumentResolver.class);
    private final AuthenticationManager authenticationManager;

    /**
     * 入参筛选
     *
     * @param methodParameter 参数集合
     * @return 格式化后的参数
     */
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(CurrentToken.class);
    }

    /**
     * @param methodParameter       入参集合
     * @param modelAndViewContainer model 和 view
     * @param nativeWebRequest      web相关
     * @param webDataBinderFactory  入参解析
     * @return 包装对象
     */
    @Override
    public Object resolveArgument(MethodParameter methodParameter,
                                  ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest,
                                  WebDataBinderFactory webDataBinderFactory) {
        CurrentToken currentTokenAnno = methodParameter.getParameterAnnotation(CurrentToken.class);
        if (null != currentTokenAnno) {
            HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
            if (null == request) {
                log.warn("Resolve Argument error, it is not  a HttpServletRequest method");
                throw new HeimdallException("Resolve Argument error, it is not  a HttpServletRequest method," +
                        "@CurrentToken applies only to Controller");
            }
            return authenticationManager.getCurrentToken(true);
        }
        return null;
    }
}
